package com.dongjiantong.biz.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.mapper.FarmStockMapper;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * Created by ThinkPad on 2017/12/6.
 */
@Slf4j
@Service(version = "1.0.0")
public class FarmStockServiceImpl extends ServiceMybatis<FarmStock> implements FarmStockService {
    @Resource
    private FarmStockMapper farmStockMapper;

    @Override
    public FarmStock selectByFarmID(Long farmID) {
        FarmStock farmStock = new FarmStock();
        farmStock.setFarm_id(farmID);
        return farmStockMapper.selectOne(farmStock);
    }

    /**
     * 根据养殖场id更改存栏
     *
     * @param stock
     * @return
     */

    @Override
    public Boolean UpdateStock(Long farmID, Integer stock) {
        FarmStock farmStock = new FarmStock();
        farmStock.setFarm_id(farmID);
        FarmStock farmStock1 = farmStockMapper.selectOne(farmStock);
        farmStock1.setReal_stock(stock);
        farmStock1.setChange_stock(stock - farmStock1.getInitial_stock());
        int a = farmStockMapper.updateByPrimaryKey(farmStock1);
        if (a == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void updateByFarmId(Map<String, Object> stockUpdateParamsMap) {
        farmStockMapper.updateByFarmId(stockUpdateParamsMap);
    }

    @Override
    public Integer selectTotalQuantity(Map map) {
        return farmStockMapper.selectTotalQuantity(map);
    }

    @Override
    public List<Map<String, Object>> getStockByBreedType(Integer provinceId, Integer cityId, Integer regionId, Integer streetId, Integer villageId) {
        Map<String , Object> paramMap = Maps.newHashMap();
        if (provinceId != null) {
            paramMap.put("provinceId",provinceId);
            if (cityId != null) {
                paramMap.put("cityId",cityId);
                if (regionId != null) {
                    paramMap.put("regionId",regionId);
                    if (streetId != null) {
                        paramMap.put("streetId",streetId);
                        if (villageId != null)
                            paramMap.put("villageId",villageId);
                    }
                }
            }
        }else {
            return null;
        }
        return farmStockMapper.getStockByBreedType(paramMap);
    }
}
